﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
using System.Data;

namespace Kea
{
    public partial class TableReaderWriter<T> where T : TableReaderWriter<T>, new()
    {
        public TableReaderWriter() { }



        internal protected virtual SqlCeConnection ConnectionGet() { throw new NotImplementedException(); }

        internal protected virtual void ConnectionRelease(SqlCeConnection cnn) { }

        private void SetValue(string fieldName, object value)
        {
            if (value == null) value = DBNull.Value;

            int ord = GetOrd(fieldName);

            if (UpdatableRecord == null)
                ResultSet.SetValue(ord, value);
            else
                UpdatableRecord.SetValue(ord, value);
        }

        private void SetNull(int ord)
        {
            if (UpdatableRecord == null)
                ResultSet.SetValue(ord, DBNull.Value);
            else
                UpdatableRecord.SetValue(ord, DBNull.Value);
        }
        private IDataRecord ReadData { get { return TRWManager.ReadData; } }
        private SqlCeResultSet ResultSet { get { return TRWManager.ResultSet; } }
        private SqlCeUpdatableRecord UpdatableRecord { get { return TRWManager.UpdatableRecord; } }

        internal TRWManager<T> TRWManager { get; set; }

        internal protected virtual string GetTableName() { return GetType().Name; }




        public static DataPlanStart<T> Read()
        {
            return new DataPlanStart<T>();
        }

        #region Create

        public static int Create<TE>(IEnumerable<TE> list, Action<TE, T> create)
        {
            using (var en = list.GetEnumerator())
            {
                if (!en.MoveNext()) return 0;

                using (var m = Create())
                {
                    int c = 0;
                    do
                    {
                        m.CreateNew();
                        create(en.Current, m.Item);
                        m.Save();
                        c++;
                    } while (en.MoveNext());
                    return c;
                }
            }
        }
        public static void CreateOne(Action<T> create)
        {
            using (var m = Create())
            {
                m.CreateNew();
                create(m.Item);
                m.Save();
            }
        }

        public static TRWManagerWithCreate<T> Create() { return new TRWManagerWithCreate<T>(); }

        #endregion


        public static DataPlanLookupable<T, TLookupValue> Lookup<TLookupValue>(Func<T, TLookupValue> lookupFunction, TLookupValue defaultValue)
        {
            return new DataPlanLookupable<T, TLookupValue>() { LookupResultFunction = lookupFunction, DefaultValue = defaultValue };
        }
        public static DataPlanLookupable<T, TLookupValue> Lookup<TLookupValue>(Func<T, TLookupValue> lookupFunction)
        {
            TLookupValue defaultValue = (TLookupValue)((typeof(TLookupValue) == typeof(string)) ? (object)string.Empty : default(TLookupValue));
            return Lookup(lookupFunction, defaultValue);
        }
        public static DataPlanLookupable<T, T> Lookup()
        {
            return Lookup(t => t);
        }









    }
}
